﻿/// <reference path="global.variable.js" />
/// <reference path="ppds.js" />
/// <reference path="jquery.validate.js" />
/// <reference path="AjaxSuccess.js" />




$(document).ready(function () {

    $(article_form).live('submit', function () {
        var form = $(article_form);
        var valid = form.validate().form();
        if (valid) {
            var action = $(form).attr("action");
            var data = $(form).serialize();

            AjaxPost(data, jQuery.CreateAjaxUrl(action), "#article", NaN, AfterArticleSucess);
        }
        return false;
    });


//    //
//    $('input[type="checkbox"].chk-graphic').live('change', function () {
//        if ($(this).is(':checked')) {
//            //  alert('hi');
//            //$('input[type="checkbox"]').hide();
//            $("INPUT[type='checkbox']").attr('checked', true);
//        }
//        else {
//            $("INPUT[type='checkbox']").attr('checked', false);
//            $("INPUT[type='checkbox']").attr('disabled', false); 
//        }
//    });

});

function AfterArticleSucess() {
    var x = $('form#article-form');
    // set loded article to reload
    if ($(x).length) {
        //var perviousPubId = $(x).find("#PerviousPublicationId").val();
        var pubId = $(x).find("#Article_PublicationId").val();
        var artId = $(x).find("#Article_Id").val();
        // if (artId != "0") 
        {
            ajaxParam.action = "/Article/index?publicationId=" + pubId + "&&articleId=" + artId;
        }
    }
}

function OpenArticle(action) {

    

    if (IsDragging) // user started dragging...show not fire the click event
     {
        IsDragging = false;
        return;
    }


    ClearMainArticleForm();
    ajaxParam.action = action;
    OpenArticleTab(ajaxParam)
    SetToOrDefaultTab(ajaxParam.tabIndexToActivate);
    //$('Article_Headline').focus();
}

// it is needed when for edit mode
function ClearMainArticleForm() {
    var x = $('form#article-form');

    if ($(x).length) {
        $(x).html('');
    }
}

//
function loadArticle() {

    var x = $('form#article-form');
    var artId = "";
    
    if ($(x).length) {
        //var perviousPubId = $(x).find("#PerviousPublicationId").val();
        artId = $(x).find("#Article_Id").val();
    }

    if (artId != "0") {
        AjaxCall(ajaxParam.action, ajaxParam.target);         
    }

    
}


function OpenArticleTab(param) {

    var artilceTabIndex = GetTabIndex(name_article);
    if (artilceTabIndex == -1) {


        var count = $(tab_id).tabs('length');
        if (count < 3) {
            $(tab_id).tabs("add", tab_2_id, name_article);
            param.target = tab_2_id;
            param.tabIndexToActivate = 2;
        } else {
            $(tab_id).tabs("add", tab_3_id, name_article);
            param.target = tab_3_id;
            param.tabIndexToActivate = 3;
        }
    }
    else {

        param.tabIndexToActivate = artilceTabIndex;
        if (artilceTabIndex == 2) {
            param.target = tab_2_id;

        }
        else {
            param.target = tab_3_id;
        }
    }

}






function AjaxPostArticle() {
    var message = $('div#ArticleMainSubmitButton').attr('message');
    var form = $(article_form);
    var oldIsReadyForGraphic = $('#OldIsReadyForGraphic').val();
    var isReadyForGraphic = $('#Article_IsReadyForGraphic').attr('checked');

    if (oldIsReadyForGraphic == 'False' && isReadyForGraphic) {
          SaveItem = function (sender) {       
            form.submit();
           };
    
            jQuery.Confirm(this, message, SaveItem);
            return false;
    }
    else{
        form.submit();
    }

  

    
    
}

